package org.example;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ycd
 */
public class ProblemFour {
    public static final Map<String, FruitPrice> FRUIT_PRICES = new HashMap<String, FruitPrice>() {{
        put("苹果", new FruitPrice(8.0, 1.0));
        put("草莓", new FruitPrice(13.0, 0.8));
        put("芒果", new FruitPrice(20.0, 1.0));
    }};
    //  加一个内部类方便使用来存储单价和折扣
    static class FruitPrice {
        double price;
        double discount; // 折扣比例，例如0.8代表八折

        FruitPrice(double price, double discount) {
            this.price = price;
            this.discount = discount;
        }
    }
    public static double calculateTotalPrice(Map<String, Integer> fruitNums) {
        double total = 0.0;
        for (Map.Entry<String, Integer> entry : fruitNums.entrySet()) {
            String fruitName = entry.getKey();
            int weight = entry.getValue();
            if (FRUIT_PRICES.containsKey(fruitName)) {
                FruitPrice fruitInfo = FRUIT_PRICES.get(fruitName);
                double discountedPrice = fruitInfo.price * fruitInfo.discount;
                total += weight * discountedPrice;
            } else {
                System.out.println("无此水果");
            }
        }
        return total;
    }
    //这里正常开发，当优惠策略多的时候我会使用策略默认封装一下
    public static double calculateTotalPriceWithDiscount(Map<String, Integer> fruitWeights) {
        double totalPriceBeforeDiscount = calculateTotalPrice(fruitWeights); // 先计算不打折的总价
        // 应用满100减10的优惠策略
        double discount = 0.0;
        if (totalPriceBeforeDiscount >= 100.0) {
            int fullReductions = (int)(totalPriceBeforeDiscount / 100); // 确定能享受多少次满减
            discount = fullReductions * 10.0; // 计算总减免金额
            totalPriceBeforeDiscount -= discount; // 从总价中扣除减免金额
        }
        return totalPriceBeforeDiscount;
    }
    public static void main(String[] args) {
        // 测试 顾客购买了1斤苹果，2斤草莓，和3斤芒果
        Map<String, Integer> fruitNums = new HashMap<String, Integer>() {{
            put("苹果", 1);
            put("草莓", 2);
            put("芒果", 4);
        }};

        double totalPrice = calculateTotalPriceWithDiscount(fruitNums);
        System.out.printf("顾客B购买的水果总价为 %.2f 元。\n", totalPrice);
    }


}